/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dtd.to.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXParseException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;

/**
 * Class DoctypeRemover
 * This class contains methods to get
 * document and also path to document,
 * which should be transformed.
 *
 * @version 1.0
 * @author tomas
 */
public class DoctypeRemover {
	
	private String XMLPath;
	private Document doc = null;
    /**
     * Constructor for Class DoctypeRemover
     * Initialize Path to XML Document.
    */ 	
	public DoctypeRemover(String Path){
		this.XMLPath = Path;
		this.setDocument();
	}
	
	public Document getDoc() {
        return doc;
    }
	
	public String getXMLPath() {
        return XMLPath;
    }

    public void setXMLPath(String Path) {
        this.XMLPath = Path;
    }
	/**
     * Sets document to remove Doctype
     * If initialization is not successful prints error message
     */ 
	private void setDocument() {
		try {			
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();			
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();			
			this.doc = docBuilder.parse (new File(XMLPath));		
		}catch (SAXParseException err) {
			 System.out.println ("Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
			  System.out.println(" " + err.getMessage ());
		}catch (SAXException | ParserConfigurationException | IOException ex){
			System.err.println(ex.getMessage());
		}
	}
    /**
     * Sets name space of document
     */	
	public void setNS() {
        File tmp = new File("output.xsd");
		Element root = getDoc().getDocumentElement();
		root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:noNamespaceSchemaLocation",tmp.getAbsolutePath());
	}
    /**
     * Remove external doctype from document
     * if is not successful prints error message 
     */ 	
	public void transformDoc() {
		try {
			Transformer xformer = TransformerFactory.newInstance().newTransformer();
			FileOutputStream fos = new FileOutputStream(new File(XMLPath));
			Result result = new StreamResult(fos);  
			Source source = new DOMSource(doc);
			xformer.transform(source, result);
		}catch (FileNotFoundException | DOMException | TransformerFactoryConfigurationError | TransformerException ex) {
			System.err.println(ex.getMessage());
		} 
	}
}
